/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.apache.passport.service  
 */
package com.apache.portal.common.oscache;

import java.util.HashMap;
import java.util.Map;

import com.apache.cache.service.CacheManager;
import com.apache.cache.service.impl.ehcache.EhcacheManagerImpl;
import com.apache.cache.service.impl.redis.JedisSsoUtil;
import com.apache.tools.StrUtil;

/**
 * description:  sso缓存工具类
 * @author qihan 创建时间：2017-9-27  
 */
public class CacheHelper {

    private static CacheHelper instance;
    private static Map<String, CacheManager> ehcaches = new HashMap<String, CacheManager>();

    private CacheHelper() {
    }

    public synchronized static CacheHelper getInstance() {
        if (null == instance) {
            instance = new CacheHelper();
        }
        return instance;
    }

    public CacheManager getCache(String cacheName) {
        cacheName = StrUtil.doNull(cacheName, "iusparamcache");
        return getEhcacheManager(cacheName);
    }

    public Object getCacheInfo(String key, String cacheName) {
        cacheName = StrUtil.doNull(cacheName, "iusparamcache");
        return getEhcacheManager(cacheName).getCacheCloneByKey(key);
    }

    /**
     * 获取ehcache缓存实例
     * @param cacheName 缓存空间名称
     * @return
     */
    private CacheManager getEhcacheManager(String cacheName) {
        if (null == ehcaches.get(cacheName)) {
            CacheManager cacheManager = new EhcacheManagerImpl();
            cacheManager.setCacheName(cacheName);
            ehcaches.put(cacheName, cacheManager);
        }
        return ehcaches.get(cacheName);
    }

    public Object getIusParamCache(String key) {
        if (JedisSsoUtil.getInstance().isRedisCache()) {
            return JedisSsoUtil.getInstance().getIusparamcacheByFileId(key);
        } else {
            return getCache("").getCacheObjectByKey(key);
        }
    }

    public void setIusParamCache(String key, Map<String, String> params) {
        if (JedisSsoUtil.getInstance().isRedisCache()) {
            JedisSsoUtil.getInstance().setIusparamcache(key, params);
        } else {
            getCache("").createCacheObject(key, params);
        }
    }
}
